import { createStore, useStore as baseUseStore } from 'vuex'
import type {Store} from "vuex"
import type {InjectionKey} from "vue";

import VuexPersistence  from "vuex-persist";

import  users from './modules/users'
import  checks from './modules/checks'
import  news from './modules/news'
import  signs from './modules/signs'

import type {UsersState} from './modules/users'
import type {NewsState} from './modules/news'
import type {ChecksState} from './modules/checks'
import type {SignsState} from './modules/signs'

export interface State{}

export interface StateAll extends State{
    users: UsersState,
    news: NewsState,
    signs: SignsState,
    checks: ChecksState
}

const vuexLocal = new VuexPersistence<State>({
    storage: window.localStorage,
    reducer: (state) => ({users: {token: (state as StateAll).users.token}
    })
})

export const key: InjectionKey<Store<StateAll>> = Symbol()
export function useStore(){
    return baseUseStore(key)
}


export default createStore({
    state: {
    },
    getters: {
    },
    mutations: {
    },
    actions: {
    },
    modules: {
      users,
      news,
      signs,
      checks
    },
    plugins: [vuexLocal.plugin],
})
